#!/bin/bash
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: yandi-first-boot.sh
# author: david921518@qq.com

################
# global vars
GLB_WORK_DIR=
GLB_DONE_FILE=
GLB_FIRSTBOOT_ROOT=
GLB_FIRSTBOOT_REBOOT_COUNTER=

#GLB_WORK_DIR=$HOME/firstboot/guest
GLB_WORK_DIR=/etc/kpc/firstboot/guest

GLB_PREPARE_FB_F=$GLB_WORK_DIR/.yandi-prepare-firstboot.stamp
GLB_PREPARE_FB_S=$GLB_WORK_DIR/yandi-prepare-firstboot.sh

GLB_FIRST_BOOT_F=$GLB_WORK_DIR/.yandi-first-boot.stamp
GLB_FIRST_BOOT_S=$GLB_WORK_DIR/yandi-first-boot.sh

GLB_SETUP_ENVIRONMENT_F=$GLB_WORK_DIR/.yandi-setup-environment.stamp
GLB_SETUP_ENVIRONMENT_S=$GLB_WORK_DIR/yandi-setup-environment.sh

GLB_SETUP_ENV2_F=$GLB_WORK_DIR/.yandi-setup-env2.stamp
GLB_SETUP_ENV2_S=$GLB_WORK_DIR/yandi-setup-env2.sh

GLB_CUSTOM_DEB_F=$GLB_WORK_DIR/.yandi-customize-debian.stamp
GLB_CUSTOM_DEB_S=$GLB_WORK_DIR/yandi-customize-debian.sh

GLB_POP_ROOT_F=$GLB_WORK_DIR/.yandi-populate-root.stamp
GLB_POP_ROOT_S=$GLB_WORK_DIR/yandi-populate-root.sh

GLB_CLEAN_SYS_F=$GLB_WORK_DIR/.yandi-cleanup-system.stamp
GLB_CLEAN_SYS_S=$GLB_WORK_DIR/yandi-cleanup-system.sh
################

user=$(whoami)

# 定义日志格式-前缀
log_prefix="[first-boot]"
# 定义日志目录和文件名
log_file_dir="/var/log/kpc"
log_file_path="$log_file_dir/firstboot.log"

log_kmsg_path="/dev/kmsg"
log_con_path="/dev/tty1"

function log_init()
{
  # 文件不存在则创建
  if [ ! -d "$log_file_dir" ]; then
    mkdir -p $log_file_dir
    chmod -R 755 $log_file_dir
  fi
  if [ ! -f "$log_file_path" ]; then
    touch $log_file_path
  fi
}

# 定义日志打印格式
function log()
{
  # 定义日志时间格式
  local curtime=$(date "+%Y-%m-%d %H:%M:%S")
  echo "$curtime $log_prefix $@"
  if [ "$user" == "root" ]; then
    echo "$curtime $log_prefix $@" >>$log_file_path
    #sleep 0.1
    echo "$log_prefix [kmsg] $@" >$log_kmsg_path
    sleep 0.1
    #echo "$curtime $log_prefix [con] $@" >$log_con_path
    #sleep 0.1
  fi
}

# loop forever to reboot
function force_reboot()
{
  log "force reboot now!"
  while true; do reboot; sleep 9; done
}

function get_work_dir()
{
  ################
  # /kpcos/distro/yandi/releases/1.52/ports/amd64/guest/yandi-first-boot.sh
  # /kpcos/distro/yandi/releases/1.52/ports/amd64/root/etc/zzz-firstboot
  ################

  local real_path=$(realpath $0)
  GLB_WORK_DIR=$(dirname "$real_path")
  log "WORK DIR is [$GLB_WORK_DIR]"

  local done_file="${real_path}_done"
  log "done_file=[$done_file]"
  real_done_file=$(realpath "$done_file")
  log "real_done_file=[$real_done_file]"

  GLB_DONE_FILE="$real_done_file"
  log "GLB_DONE_FILE=[$GLB_DONE_FILE]"

  local firstboot_root="$GLB_WORK_DIR/../root"
  log "firstboot_root=[$firstboot_root]"
  real_firstboot_root=$(realpath "$firstboot_root")
  log "real_firstboot_root=[$real_firstboot_root]"

  if [ ! -d $real_firstboot_root ]; then
    log "No firstboot root dir [$real_firstboot_root]."
    exit 1
  fi

  GLB_FIRSTBOOT_ROOT="$real_firstboot_root"
  log "GLB_FIRSTBOOT_ROOT=[$GLB_FIRSTBOOT_ROOT]"

  GLB_PREPARE_FB_F=$GLB_WORK_DIR/.yandi-prepare-firstboot.stamp
  GLB_PREPARE_FB_S=$GLB_WORK_DIR/yandi-prepare-firstboot.sh
  log "GLB_PREPARE_FB_F=$GLB_PREPARE_FB_F"
  log "GLB_PREPARE_FB_S=$GLB_PREPARE_FB_S"

  GLB_FIRST_BOOT_F=$GLB_WORK_DIR/.yandi-first-boot.stamp
  GLB_FIRST_BOOT_S=$GLB_WORK_DIR/yandi-first-boot.sh
  log "GLB_FIRST_BOOT_F=$GLB_FIRST_BOOT_F"
  log "GLB_FIRST_BOOT_S=$GLB_FIRST_BOOT_S"

  GLB_SETUP_ENVIRONMENT_F=$GLB_WORK_DIR/.yandi-setup-environment.stamp
  GLB_SETUP_ENVIRONMENT_S=$GLB_WORK_DIR/yandi-setup-environment.sh
  log "GLB_SETUP_ENVIRONMENT_F=$GLB_SETUP_ENVIRONMENT_F"
  log "GLB_SETUP_ENVIRONMENT_S=$GLB_SETUP_ENVIRONMENT_S"

  GLB_SETUP_ENV2_F=$GLB_WORK_DIR/.yandi-setup-env2.stamp
  GLB_SETUP_ENV2_S=$GLB_WORK_DIR/yandi-setup-env2.sh
  log "GLB_SETUP_ENV2_F=$GLB_SETUP_ENV2_F"
  log "GLB_SETUP_ENV2_S=$GLB_SETUP_ENV2_S"

  GLB_CUSTOM_DEB_F=$GLB_WORK_DIR/.yandi-customize-debian.stamp
  GLB_CUSTOM_DEB_S=$GLB_WORK_DIR/yandi-customize-debian.sh
  log "GLB_CUSTOM_DEB_F=$GLB_CUSTOM_DEB_F"
  log "GLB_CUSTOM_DEB_S=$GLB_CUSTOM_DEB_S"

  GLB_POP_ROOT_F=$GLB_WORK_DIR/.yandi-populate-root.stamp
  GLB_POP_ROOT_S=$GLB_WORK_DIR/yandi-populate-root.sh
  log "GLB_POP_ROOT_F=$GLB_POP_ROOT_F"
  log "GLB_POP_ROOT_S=$GLB_POP_ROOT_S"

  GLB_CLEAN_SYS_F=$GLB_WORK_DIR/.yandi-cleanup-system.stamp
  GLB_CLEAN_SYS_S=$GLB_WORK_DIR/yandi-cleanup-system.sh
  log "GLB_CLEAN_SYS_F=$GLB_CLEAN_SYS_F"
  log "GLB_CLEAN_SYS_S=$GLB_CLEAN_SYS_S"

  GLB_FIRSTBOOT_REBOOT_COUNTER=$GLB_WORK_DIR/reboot_counter
  log "GLB_FIRSTBOOT_REBOOT_COUNTER=$GLB_FIRSTBOOT_REBOOT_COUNTER"
  if [ ! -f $GLB_FIRSTBOOT_REBOOT_COUNTER ]; then
    echo "0" >$GLB_FIRSTBOOT_REBOOT_COUNTER
  fi
}

function check_reboot_counter()
{
  local reboot_counter=$(cat $GLB_FIRSTBOOT_REBOOT_COUNTER)
  ((reboot_counter++))
  log "firstboot reboot counter is [$reboot_counter]"
  if [ $reboot_counter -gt 9 ]; then
    log "firstboot reboot too many times, exit now!"
    exit 1
  else
    log "set firstboot reboot counter to [$reboot_counter]"
    echo "$reboot_counter" >$GLB_FIRSTBOOT_REBOOT_COUNTER
  fi
}

function reset_reboot_counter()
{
  log "reset firstboot reboot counter"
  echo "0" >$GLB_FIRSTBOOT_REBOOT_COUNTER
}

# 0. init script
log_init
log "[first-boot] START"
get_work_dir
if [ -f $GLB_DONE_FILE ]; then
  log "exist file $GLB_DONE_FILE , exit now"
  exit 1
fi

if [ ! -f $GLB_FIRST_BOOT_F ]; then
  log "start yandi-first-boot.sh"
  check_reboot_counter

  # 0. prepare first boot
  if [ -x $GLB_PREPARE_FB_S ]; then
    if [ ! -f $GLB_PREPARE_FB_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-prepare-firstboot.sh"
      $GLB_PREPARE_FB_S
      touch $GLB_PREPARE_FB_F
      log "finish yandi-prepare-firstboot.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      #force_reboot
    fi
  fi

  # 1. setup environment first stage
  if [ -x $GLB_SETUP_ENVIRONMENT_S ]; then
    if [ ! -f $GLB_SETUP_ENVIRONMENT_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-setup-environment.sh"
      $GLB_SETUP_ENVIRONMENT_S
      touch $GLB_SETUP_ENVIRONMENT_F
      log "finish yandi-setup-environment.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      force_reboot
    fi
  fi

  # 2. setup environment second stage
  if [ -x $GLB_SETUP_ENV2_S ]; then
    if [ ! -f $GLB_SETUP_ENV2_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-setup-env2.sh"
      $GLB_SETUP_ENV2_S
      touch $GLB_SETUP_ENV2_F
      log "finish yandi-setup-env2.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      force_reboot
    fi
  fi

  # 3. customize debian
  if [ -x $GLB_CUSTOM_DEB_S ]; then
    if [ ! -f $GLB_CUSTOM_DEB_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-customize-debian.sh"
      $GLB_CUSTOM_DEB_S
      touch $GLB_CUSTOM_DEB_F
      log "finish yandi-customize-debian.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      force_reboot
    fi
  fi

  # 4. populate root
  if [ -x $GLB_POP_ROOT_S ]; then
    if [ ! -f $GLB_POP_ROOT_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-populate-root.sh"
      $GLB_POP_ROOT_S
      touch $GLB_POP_ROOT_F
      log "finish yandi-populate-root.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      force_reboot
    fi
  fi

  # 5. clean up system
  if [ -x $GLB_CLEAN_SYS_S ]; then
    if [ ! -f $GLB_CLEAN_SYS_F ]; then
      cd $GLB_WORK_DIR
      log "start yandi-cleanup-system.sh"
      $GLB_CLEAN_SYS_S
      touch $GLB_CLEAN_SYS_F
      log "finish yandi-cleanup-system.sh"
      log ""
      log ""
      log ""
      reset_reboot_counter
      force_reboot
    fi
  fi

  touch $GLB_FIRST_BOOT_F
  log "do nothing to finish yandi-first-boot.sh"
  log ""
  log ""
  log ""
fi

touch $GLB_DONE_FILE
sync

log "[first-boot] EXIT"
log ""
log ""
log ""

